package com.canbot.u05.activity.customvoice.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.RelativeLayout
import android.widget.TextView
import com.canbot.u05.R
import com.canbot.u05.activity.customvoice.bean.QuestionBean
import com.canbot.u05.activity.customvoice.tool.CheckStringUtils
import com.canbot.u05.utils.Logger
import com.canbot.u05.utils.ToastTools
import kotlinx.android.synthetic.main.custom_dialog_add_question_item.view.*

/**
 * CustomDialogAddQuestionAdapter
 *
 * @author  Liu.Heng
 * @date  2019/5/30
 */
class CustomDialogAddQuestionAdapter(val context: Context, var list: MutableList<QuestionBean>, var call: SaveEditListener?) : RecyclerView.Adapter<CustomDialogAddQuestionAdapter.MyViewHolder>() {

    private val layoutInflater: LayoutInflater = LayoutInflater.from(context)

    private var mList = list

    interface SaveEditListener {

        /**
         * 回调给主界面方法(添加问题)
         *
         * @param position 下标
         * @param string   字符串
         */
        fun saveEdit(position: Int, string: String, boolean: Boolean)

        /**
         * 回调给主界面方法(移除)
         *
         * @param position 下标
         */
        fun removeData(position: Int)
    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
        val inflate = layoutInflater.inflate(R.layout.custom_dialog_add_question_item, p0, false)
        return MyViewHolder(inflate)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    fun removeItem(position: Int) {
        notifyItemRemoved(position)
        notifyItemRangeChanged(position, mList.size - position)
    }

    override fun onBindViewHolder(myViewHolder: MyViewHolder, @SuppressLint("RecyclerView") position: Int) {
        val delete1 = myViewHolder.delete1
        val delete2 = myViewHolder.delete2
        val editText = myViewHolder.editText
        val redRelativeLayout = myViewHolder.redRelativeLayout
        val mindText = myViewHolder.mindText

        if (editText.tag is TextWatcher) {
            editText.removeTextChangedListener(editText.tag as TextWatcher)
        }

        val i = position + 1
        editText.hint = "问题$i"
        editText.setText(mList[position].question)

        if (mList[position].question.isEmpty()) {
            delete2.visibility = View.GONE
            mindText.text = ""
            redRelativeLayout.background = context.getDrawable(R.drawable.custom_dialog2_question_box)
        } else {
//            if (CheckStringUtils.isChineseOrEnglish(editText.text.toString())) {
                if (editText.text.toString().length < 2) {
                    redRelativeLayout.background = context.getDrawable(R.drawable.custom_dialog2_question_red)
                    mindText.text = "最少2个字"
                    call?.saveEdit(position, editText.text.toString(), true)
                } else {
                    redRelativeLayout.background = context.getDrawable(R.drawable.custom_dialog2_question_box)
                    mindText.text = ""
                    call?.saveEdit(position, editText.text.toString(), true)
                }
//            } else {
//                redRelativeLayout.background = context.getDrawable(R.drawable.custom_dialog2_question_red)
//                mindText.text = "只支持汉字"
//                call?.saveEdit(position, editText.text.toString(), true)
//            }
        }

        editText.setOnFocusChangeListener { v, b ->
            Logger.e("editText", "结束编辑$v$b")
            if (editText.text.toString().isNotEmpty()) {
                editText.setSelection(editText.text.length)
                if (b) {
                    delete2.visibility = View.VISIBLE
                } else {
                    delete2.visibility = View.GONE
                }
            }
        }

        //添加editText的监听事件
        val textWatcher = object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
            }

            override fun onTextChanged(s: CharSequence, i: Int, i1: Int, i2: Int) {
                val size = 20
                if (s.length > size) {
                    editText.setText(s.subSequence(0, size))
                    editText.setSelection(size)
                    ToastTools.newInstance(context.applicationContext).showText("最大输入20个字哦！")
                }
            }

            override fun afterTextChanged(editable: Editable) {
                //用户输入完毕后，处理输入数据，回调给主界面处理
                if (editText.text.toString().isEmpty()) {
                    delete2.visibility = View.GONE
                    redRelativeLayout.background = context.getDrawable(R.drawable.custom_dialog2_question_box)
                    mindText.text = ""
                } else {
                    delete2.visibility = View.VISIBLE
//                    if (CheckStringUtils.isChineseOrEnglish(editText.text.toString())) {
                        if (editText.text.toString().length < 2) {
                            redRelativeLayout.background = context.getDrawable(R.drawable.custom_dialog2_question_red)
                            mindText.text = "最少2个字"
                            call?.saveEdit(position, editText.text.toString(), true)
                        } else {
                            redRelativeLayout.background = context.getDrawable(R.drawable.custom_dialog2_question_box)
                            mindText.text = ""
                            call?.saveEdit(position, editText.text.toString(), true)
                        }
//                    } else {
//                        redRelativeLayout.background = context.getDrawable(R.drawable.custom_dialog2_question_red)
//                        mindText.text = "只支持汉字"
//                        call?.saveEdit(position, editText.text.toString(), true)
//                    }
                }
            }

        }

        editText.addTextChangedListener(textWatcher)
        editText.tag = textWatcher

        delete1.setOnClickListener {
            call?.removeData(position)
        }

        delete2.setOnClickListener {
            editText.setText("")
            //用户清楚完毕后，处理数据，回调给主界面处理
            call?.saveEdit(position, editText.text.toString(), true)
        }
    }

    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var delete1: Button = view.delete1
        var delete2: Button = view.delete2
        var editText: EditText = view.my_editText
        var redRelativeLayout: RelativeLayout = view.red_relativeLayout
        var mindText: TextView = view.mind_text
    }
}